package com.tiantianyuegou.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.R;
import com.tiantianyuegou.adapter.MyEvaluateAdapter;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.http.SPHelper;
import com.tiantianyuegou.model.PingjiaModel;
import com.tiantianyuegou.util.LoadingDialog;
import com.tiantianyuegou.util.Tools;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 我的评价（发表评价）
 */
public class MyEvaluateActivity extends BaseActivity implements View.OnClickListener {
    @Override
    public void findViewByIds() {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {

    }

   /* private ImageView img_title;
    private TextView text_title;

    private TextView text_fabiao;

    private String TAG = "MyEvaluateActivity";
    private ListView list_evaluate;
    private MyEvaluateAdapter myEvaluateAdapter;


    private String tpStr;

    private List<PingjiaModel> mData;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_evaluate);
        initViews();
       Tools.changeSystemBar(this);
    }

    @Override
    public void findViewByIds() {
        text_title = (TextView) findViewById(R.id.text_title);
        img_title = (ImageView) findViewById(R.id.img_title);
        text_title.setText(R.string.evaluate_title);
        text_fabiao = (TextView) findViewById(R.id.text_fabiao);
        list_evaluate = (ListView) findViewById(R.id.list_evaluate);
    }

    @Override
    public void initListener() {
        img_title.setOnClickListener(this);
        text_fabiao.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mData = new ArrayList<>();
        myEvaluateAdapter = new MyEvaluateAdapter(this);
        list_evaluate.setAdapter(myEvaluateAdapter);
        try {
            JSONObject root = new JSONObject(getIntent().getStringExtra("products"));
            JSONArray list = root.getJSONArray("products");
            if (list.length() > 0) {
                for (int i = 0; i < list.length(); i++) {
                    JSONObject value = list.getJSONObject(i);
                    PingjiaModel model = new PingjiaModel();
                    Double price = Double.valueOf(value.getString("todPrice"));
//                    model.setPrice("￥" + new DecimalFormat("0.00").format(price));
//                    model.setCount("x" + value.getString("todCount"));
//                    model.setEvaluateId(value.getString("tpId"));
//                    model.setPic(value.getString("tpPic"));
                    model.setName(value.getString("tpName"));
                    if (value.getString("todSpecJson").contains("[")) {
                        JSONArray specs = new JSONArray(value.getString("todSpecJson"));
                        StringBuffer spec = new StringBuffer();
                        for (int j = 0; j < specs.length(); j++) {
                            JSONObject specValue = specs.getJSONObject(j);
                            spec.append(specValue.getString("tsName")).append(":");
                            spec.append(specValue.getString("value")).append(" ");
                        }
//                        model.setSpec(spec.toString());
                    }
                    mData.add(model);
                }
                myEvaluateAdapter.setData(mData);
                myEvaluateAdapter.notifyDataSetChanged();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_title:
                finish();
                break;

            case R.id.text_fabiao:
                boolean isNotFinish = false;
                List<PingjiaModel> data = myEvaluateAdapter.getData();
                for(int i=0;i<data.size();i++){
                    PingjiaModel model = data.get(i);
//                    Log.e("TAG", "evaluate:" + model.getEvaluateLevel() + ":" + model.getEvaluateContent());
                }

              *//*  for (int i = 0; i < data.size(); i++) {
                    PingjiaModel model = data.get(i);
                    if (model.getEvaluateLevel() == null || "".equals(model.getEvaluateLevel()) ) {
                        isNotFinish = true;
                        break;
                    }
                }
                if (!isNotFinish) {
                    submitEvaluate();
                } else {
                    showMsg("请先完成订单评价");
                }*//*
                break;
            default:
                break;
        }
    }

    private void submitEvaluate() {
        List<PingjiaModel> data = myEvaluateAdapter.getData();
        StringBuffer ids = new StringBuffer();
        StringBuffer contents = new StringBuffer();
        StringBuffer rates = new StringBuffer();
       *//* for (int i = 0; i < data.size(); i++) {
            PingjiaModel model = data.get(i);
            ids.append(model.getEvaluateId()).append(",");
            if (model.getEvaluateContent() == null && "".equals(model.getEvaluateContent())) {
                contents.append("null|");
            } else {
                contents.append(model.getEvaluateContent()).append("|");
            }

            rates.append(model.getEvaluateLevel()).append(",");
        }*//*

        final LoadingDialog loadingDialog = new LoadingDialog(this);
        loadingDialog.setMsg("正在提交评价...");
        loadingDialog.show();

        RequestParams params = new RequestParams();
        params.addBodyParameter("tmId", SPHelper.getTmId());
        params.addBodyParameter("tpId", ids.substring(0, ids.length() - 1));
        params.addBodyParameter("toId", SPHelper.getToId());
        params.addQueryStringParameter("content", contents.substring(0, contents.length() - 1));
        params.addBodyParameter("rate", rates.substring(0, rates.length() - 1));

        Log.e("TAG", "tmId:" + SPHelper.getTmId() + ":tpId:" + ids.substring(0, ids.length() - 1) +
                ":toId:" + SPHelper.getToId() + ":content:" + contents.substring(0, contents.length() - 1) +
                ":rate:" + rates.substring(0, rates.length() - 1));

        loadData(HttpRequest.HttpMethod.POST, ApiDefine.GOODS_COMMENT, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String response = responseInfo.result;
                Log.e(TAG, TAG + "     " + response);
                loadingDialog.dismiss();
                try {
                    JSONObject object = new JSONObject(response);
                    showMsg(object.getString("message"));
                    if ("100".equals(object.getString("code"))) {
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                loadingDialog.dismiss();
                showMsg("当前网络不可用,请检查您的网络设置");
            }
        });
    }*/
}